package com.ucar.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.annotation.SuppressLint;
import android.text.TextUtils;

/**
 * 
 * @author <a href="mailto:kris1987@qq.com">Kris.lee</a>
 * @website www.krislq.com
 * @date Nov 20, 2012
 * @version 1.0.0
 * 
 */
@SuppressLint("SimpleDateFormat")
public class DateUtil {
	public static final long MINUTE_MILLIS = 60 * 1000;
	public static final long HOUR_MILLIS = MINUTE_MILLIS * 60;
	/**
	 * milliseconds of a day
	 */
	public static final long DAY_MILLIS = HOUR_MILLIS * 24;
	
	public static final long DAY_DATE = 24;
	/**
	 * milliseconds of a half day
	 */
	public static final long HALF_DAY_MILLIS = DAY_MILLIS / 2;
	/**
	 * milliseconds of a week
	 */
	public static final long WEEK_MILLIS = DAY_MILLIS * 7;
	/**
	 * milliseconds of a month
	 */
	public static final long MONTH_MILLIS = WEEK_MILLIS * 30;
	public static final long HALF_MONTH_MILLIS = MONTH_MILLIS / 2;
	/**
	 * yyyyMMdd
	 */
	public static final String DATE_DEFAULT_FORMATE = "yyyyMMdd";
	/**
	 * yyyy-MM-dd HH:mm:ss 2010-05-11 17:22:26
	 */
	public static final String DATE_FORMATE_ALL = "yyyy-MM-dd HH:mm:ss";
	/**
	 * yyyy-MM-dd HH:mm:ss 2010.05.11 17:22:26
	 */
	public static final String DATE_FORMATE_ALL_TRS = "yyyy.MM.dd HH:mm:ss";
	
	/**
	 * yyyy-MM-dd HH:mm:ss 2010.05.11 17:22:26
	 */
	public static final String DATE_FORMATE_ALL_NEW = "yyyy-MM-dd HH:mm:ss.SSS";
	
	
	/**
	 * dd/MM/yyyy, hh:mm
	 */
	public static final String DATE_FORMATE_TRANSACTION = "dd/MM/yyyy, hh:mm";
	/**
	 * MM/dd HH:mm
	 */
	public static final String DATE_FORMATE_DAY_HOUR_MINUTE = "MM/dd HH:mm";
	/**
	 * HH:mm
	 */
	public static final String DATE_FORMATE_HOUR_MINUTE = "HH:mm";
	public static final String DATE_FORMATE_HOUR_MINUTE_SECOND = "HH:mm:ss";

	public static SimpleDateFormat dateFormate = new SimpleDateFormat();

	
	 /**
     * 获得当前日期时间
     * @return 日期时间
     */
    public static String getCurDateTime()
    {
    	Calendar calendar = Calendar.getInstance();
    	int year = calendar.get( Calendar.YEAR );
    	int month = calendar.get( Calendar.MONTH ) + 1;
    	int day = calendar.get( Calendar.DAY_OF_MONTH );
    	int hour = calendar.get( Calendar.HOUR_OF_DAY );
    	int minute = calendar.get( Calendar.MINUTE );
    	int second = calendar.get( Calendar.SECOND );
    	return year + "-" + String.format("%02d", month) + "-" + String.format("%02d", day) 
    		+ " " + String.format("%02d", hour) + ":" + String.format("%02d", minute) + ":" + String.format("%02d", second) ;
    }
	/**
	 * 
	 * @param milliseconds
	 * @return the time formated by "yyyy-MM-dd HH:mm:ss"
	 */
	public static String toTime(long milliseconds) {
		return toTime(new Date(milliseconds), DATE_FORMATE_ALL);
	}

	/**
	 * 
	 * @param milliseconds
	 * @param pattern
	 * @return the time formated
	 */
	public static String toTime(long milliseconds, String pattern) {
		return toTime(new Date(milliseconds), pattern);
	}

	/**
	 * 
	 * @param date
	 * @param pattern
	 * @return
	 */
	public static String toTime(Date date, String pattern) {
		if (TextUtils.isEmpty(pattern)) {
			pattern = DATE_DEFAULT_FORMATE;
		}
		dateFormate.applyPattern(pattern);
		if (date == null) {
			date = new Date();
		}
		try {
			return dateFormate.format(date);
		} catch (Exception e) {
			return "";
		}
	}

	public static long toMinuteMillis(int milis) {
		long tempMillis;
		tempMillis = milis * HOUR_MILLIS * MINUTE_MILLIS;

		return tempMillis;

	}
	
	/**
	 * 计算间隔时间
	 * 
	 * @param loadTime
	 */
	public static String getIntervalTime2(String loadTime) {
		String time = null;
		try {
			long now = System.currentTimeMillis();
			long createdTime = new SimpleDateFormat(DATE_FORMATE_ALL_NEW).parse(loadTime)
					.getTime();
			Calendar calendar = Calendar.getInstance();
			calendar.setTimeInMillis(createdTime);
			
			// System.out.println("间隔时间：" + (now - s) / 1000 / 60 / 60);
			long tempMinute = (now - createdTime) / 1000 / 60;
			long tempTime = tempMinute / 60;
			
			// 如果大于三天
			if (tempTime >= DAY_DATE * 3) 
			{
				int loadYear = calendar.get(Calendar.YEAR);
				calendar.setTimeInMillis(now);
				int currentYear = calendar.get(Calendar.YEAR);
				calendar.setTimeInMillis(createdTime);

				// 如果在本年度内,输出MM-dd HH:mm
				if(loadYear == currentYear)
				{
					time = String.format( "%tm" , calendar) + "-"
							+ String.format( "%td" , calendar) + " "
							+ String.format( "%tR" , calendar);
				}
				// 如果在本年度以外,输出yyyy-MM-dd HH:mm
				else
				{
					time = String.format( "%tF" , calendar) + " "
							+ String.format( "%tR" , calendar);
				}
			}
			// 如果大于等于2天
			else if (tempTime >= DAY_DATE && tempTime < DAY_DATE * 3)
			{
				int loadDay = calendar.get(Calendar.DAY_OF_MONTH);
				calendar.set(Calendar.DATE, 1);//把日期设置为当月第一天  
				calendar.roll(Calendar.DATE, -1);//日期回滚一天，也就是最后一天  
			    int maxDate = calendar.get(Calendar.DATE);  
				calendar.setTimeInMillis(now);
				int currentDay = calendar.get(Calendar.DAY_OF_MONTH);
				calendar.setTimeInMillis(createdTime);
				
				if(currentDay - loadDay < 0)
				{
					loadDay = maxDate - loadDay;
				}
				
				// 如果当前天大于加载时间1天
				if((currentDay - loadDay) == 1)
				{
					time = "昨天" + String.format( "%tR" , calendar);
				}
				// 如果当前天大于加载时间2天
				else if((currentDay - loadDay) == 2)
				{
					time = "前天" + String.format( "%tR" , calendar);
				}
				// 如果当前天大于加载时间2天以上
				else if((currentDay - loadDay) > 2)
				{
					time = String.format( "%tm" , calendar) + "-"
							+ String.format( "%td" , calendar) + " "
							+ String.format( "%tR" , calendar);
				}
			}
			// 如果在1天内
			else if (tempTime > 0)
			{
				time = tempTime + "小时前";
			}
			// 如果在1小时内
			else
			{
				time = tempMinute + "分钟前";
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return time;

	}

}
